package chapter07

import scala.collection.mutable

/**
 * 合并MAp
 */
object Test16_MergeMap {
  def main(args: Array[String]): Unit = {

    val map = Map("a" -> 1, "b" -> 3, "c" -> 6)
    val map2 = mutable.Map("a" -> 6, "b" -> 2, "c" -> 9, "d" -> 3)

    //初始map2
    val map3: mutable.Map[String, Int] = map.foldLeft(map2)(
      //更新里面的值
      (mergedMap, kv) => {
      val key = kv._1
      val value = kv._2
      //mergedMap.getOrElse 取当前的值 +value
      mergedMap(key) = mergedMap.getOrElse(key, 0) + value
      mergedMap
    })

    println(map3)

  }
}
